package com.gu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gu.VO.PropertyVO;
import com.gu.VO.ServerResponse;
import com.gu.VO.UserVO;
import com.gu.dao.ApartmentDAO;
import com.gu.dao.PropertyDAO;
import com.gu.dao.UserDAO;
import com.gu.entity.ApartmentDO;
import com.gu.entity.PropertyDO;
import com.gu.entity.UserDO;
import com.gu.enumeration.RepairStatusEnum;
import com.gu.enumeration.RepairTypeEnum;
import com.gu.enumeration.UserOccupationTypeEnum;
import com.gu.enumeration.UserStatusEnum;
import com.gu.service.PropertyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: apartment-management-center
 * @Package: com.gu.service.impl
 * @ClassName: PropertyServiceImpl
 * @Author: gusongliao
 * @Description: 物业维修实现类
 * @createDate 2021/04/13 13:25
 */

@Service
public class PropertyServiceImpl implements PropertyService {

    @Autowired
    private PropertyDAO propertyDAO;

    @Autowired
    private UserDAO userDAO;

    @Autowired
    private ApartmentDAO apartmentDAO;


    @Override
    public PageInfo propertyList(Integer pageNum, Integer pageSize, String selectUserParam,Integer id,Integer repairStatus) {
        PageHelper.startPage(pageNum,pageSize);
        List<PropertyDO> propertyDOS = propertyDAO.propertyList(selectUserParam,id,repairStatus);
        PageInfo pageInfo = PageInfo.of(propertyDOS);
        List<UserDO> userDOList = userDAO.getAllUser();
        List<ApartmentDO> apartmentDOList = apartmentDAO.getAllApartment();
        List<PropertyVO> propertyVOS = new ArrayList<>();
        propertyDOS.forEach(e ->{
            PropertyVO propertyVO = new PropertyVO();
            BeanUtils.copyProperties(e,propertyVO);
            userDOList.forEach(f ->{
                propertyVO.setRepairStatus(RepairStatusEnum.getRepairStatus(e.getRepairStatus()));
                propertyVO.setRepairType(RepairTypeEnum.getRepairType(e.getRepairType()));
                //业主信息
                if (propertyVO.getOwnerId() != null && propertyVO.getOwnerId().equals(f.getId())){
                    propertyVO.setUserName(f.getUserName());
                    propertyVO.setImgPath(f.getImgPath());
                    propertyVO.setUserPhone(f.getUserPhone());
                }

                //维修人员信息
                if (propertyVO.getEngineerCode() != null && propertyVO.getEngineerCode().equals(f.getId())){
                    propertyVO.setEngineerName(f.getUserName());
                    propertyVO.setRepairPhone(f.getUserPhone());
                }
            });
            propertyVOS.add(propertyVO);
        });
        propertyVOS.forEach(e ->{
            apartmentDOList.forEach(f ->{
                if (e.getApartmentCode().equals(f.getId())){
                    e.setApartmentName("" + f.getBuildingNumber() + "-" + f.getUnitNumber() + "-" +  f.getFloorNumber() + "-" +  f.getRoomNumber());
                }
            });
        });
        pageInfo.setList(propertyVOS);
        return pageInfo;
    }

    @Override
    public ServerResponse updateRepairStatus(PropertyDO propertyDO) {
        int result = propertyDAO.updateRepairStatus(propertyDO);
        if (result == 1){
            return ServerResponse.SUCCESS;
        }
        return ServerResponse.error(19,"更新物业维修状态失败");
    }

    @Override
    public PageInfo propertyListNew(Integer pageNum, Integer pageSize,Integer repairStatus) {
        PageHelper.startPage(pageNum,pageSize);
        List<PropertyDO> propertyDOS = propertyDAO.propertyListNew(repairStatus);
        PageInfo pageInfo = PageInfo.of(propertyDOS);
        List<UserDO> userDOList = userDAO.getAllUser();
        List<ApartmentDO> apartmentDOList = apartmentDAO.getAllApartment();
        List<PropertyVO> propertyVOS = new ArrayList<>();
        propertyDOS.forEach(e ->{
            PropertyVO propertyVO = new PropertyVO();
            BeanUtils.copyProperties(e,propertyVO);
            userDOList.forEach(f ->{
                propertyVO.setRepairStatus(RepairStatusEnum.getRepairStatus(e.getRepairStatus()));
                propertyVO.setRepairType(RepairTypeEnum.getRepairType(e.getRepairType()));
                //业主信息
                if (propertyVO.getOwnerId() != null && propertyVO.getOwnerId().equals(f.getId())){
                    propertyVO.setUserName(f.getUserName());
                    propertyVO.setImgPath(f.getImgPath());
                    propertyVO.setUserPhone(f.getUserPhone());
                }

                //维修人员信息
                if (propertyVO.getEngineerCode() != null && propertyVO.getEngineerCode().equals(f.getId())){
                    propertyVO.setEngineerName(f.getUserName());
                    propertyVO.setRepairPhone(f.getUserPhone());
                }
            });
            propertyVOS.add(propertyVO);
        });
        propertyVOS.forEach(e ->{
            apartmentDOList.forEach(f ->{
                if (e.getApartmentCode().equals(f.getId())){
                    e.setApartmentName("" + f.getBuildingNumber() + "-" + f.getUnitNumber() + "-" +  f.getFloorNumber() + "-" +  f.getRoomNumber());
                }
            });
        });
        pageInfo.setList(propertyVOS);
        return pageInfo;
    }

    @Override
    public PageInfo propertyListByUserId(Integer pageNum, Integer pageSize,Integer ownerId, Integer repairStatus) {
        PageHelper.startPage(pageNum,pageSize);
        List<PropertyDO> propertyDOS = propertyDAO.propertyListByUserId(ownerId, repairStatus);
        PageInfo pageInfo = PageInfo.of(propertyDOS);
        List<UserDO> userDOList = userDAO.getAllUser();
        List<ApartmentDO> apartmentDOList = apartmentDAO.getAllApartment();
        List<PropertyVO> propertyVOS = new ArrayList<>();
        propertyDOS.forEach(e ->{
            PropertyVO propertyVO = new PropertyVO();
            BeanUtils.copyProperties(e,propertyVO);
            userDOList.forEach(f ->{
                propertyVO.setRepairStatus(RepairStatusEnum.getRepairStatus(e.getRepairStatus()));
                propertyVO.setRepairType(RepairTypeEnum.getRepairType(e.getRepairType()));
                //业主信息
                if (propertyVO.getOwnerId() != null && propertyVO.getOwnerId().equals(f.getId())){
                    propertyVO.setUserName(f.getUserName());
                    propertyVO.setImgPath(f.getImgPath());
                    propertyVO.setUserPhone(f.getUserPhone());
                }

                //维修人员信息
                if (propertyVO.getEngineerCode() != null && propertyVO.getEngineerCode().equals(f.getId())){
                    propertyVO.setEngineerName(f.getUserName());
                    propertyVO.setRepairPhone(f.getUserPhone());
                }
            });
            propertyVOS.add(propertyVO);
        });
        propertyVOS.forEach(e ->{
            apartmentDOList.forEach(f ->{
                if (e.getApartmentCode().equals(f.getId())){
                    e.setApartmentName("" + f.getBuildingNumber() + "-" + f.getUnitNumber() + "-" +  f.getFloorNumber() + "-" +  f.getRoomNumber());
                }
            });
        });
        pageInfo.setList(propertyVOS);
        return pageInfo;
    }

    @Override
    public ServerResponse insert(PropertyDO entity) {
        int result = propertyDAO.insert(entity);
        if (result == 1){
            return ServerResponse.SUCCESS;
        }
        return ServerResponse.error(21,"新增维修信息失败");
    }

    @Override
    public ServerResponse deleteByIds(Integer[] ids) {
        Integer result = propertyDAO.deleteByIds(ids);
        if (result == ids.length) {
            return ServerResponse.SUCCESS;
        }
        return ServerResponse.error(22, "删除维修消息失败");
    }
}
